package com.ctgu.controller;


import com.ctgu.service.CourseService;
import com.ctgu.service.HomeworkDetailService;
import com.ctgu.service.HomeworkService;
import com.ctgu.service.ScoreService;
import com.ctgu.vo.Course;
import com.ctgu.vo.Homework;
import com.ctgu.vo.HomeworkDetail;
import com.ctgu.vo.Score;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private HomeworkService homeworkService;

    @Resource
    private CourseService courseService;

    @Resource
    private ScoreService scoreService;

    @Resource
    private HomeworkDetailService homeworkDetailService;

    //查看已布置的作业
    @RequestMapping(value = "/viewHomeworkList")
    public String viewHomeworkList(HttpServletRequest request, Model model){
        Integer teacherId = (Integer) request.getSession().getAttribute("userId");
        List<Homework> homeworkList = homeworkService.selectHomeworkByTeacherId(teacherId);
        model.addAttribute("homeworkList",homeworkList);
        System.out.println(homeworkList.size());
        System.out.println(homeworkList.get(0).getHomeworkTitle());
        return "teacher/teacherHomework";
    }
    //学生作业提交详情
    @RequestMapping("/HomeworkSubmitDetail")
    public String HomeworkSubmitDetail(HttpServletRequest request, Model model,String homeworkId,String courseName,String homeworkTitle){
        int homeworkIdTemp = Integer.parseInt(homeworkId);
        request.getSession().setAttribute("courseName",courseName);
        request.getSession().setAttribute("homeworkTitle",homeworkTitle);
        List<HomeworkDetail> list = homeworkDetailService.StudentHomeworkDetail(homeworkIdTemp);
        model.addAttribute("list",list);
        return "teacher/teacherHomeworkSubmit";
    }
    //学生作业详情
    @RequestMapping("/HomeworkDetail")
    public String updateHomework(HttpServletRequest request,HttpServletResponse response,Model model, String courseName, String homeworkTitle, String homeworkId, String studentId){
        request.getSession().setAttribute("courseName",courseName);
        request.getSession().setAttribute("homeworkTitle",homeworkTitle);
        request.getSession().setAttribute("homeworkId",homeworkId);
        request.getSession().setAttribute("studentId",studentId);
        int studentIdTemp = Integer.parseInt(studentId);
        int homeworkIdTemp = Integer.parseInt(homeworkId);
        HomeworkDetail homeworkDetail = homeworkDetailService.selectByPrimaryKey(homeworkIdTemp,studentIdTemp);
        model.addAttribute("homeworkDetail",homeworkDetail);
        System.out.println(homeworkDetail.getTheDetail());
        return "teacher/teacherHomeworkDetail";
    }
    //提交作业等级
    @RequestMapping("/updateHomeworkLevel")
    public String updateHomeworkLevel(HttpServletRequest request){
        int homeworkId = Integer.parseInt(request.getParameter("homeworkId"));
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        String homeworkTitle = request.getParameter("homeworkTitle");
        String courseName = request.getParameter("courseName");
        String level = request.getParameter("sex");
        homeworkDetailService.updateByPrimaryKey(homeworkId,studentId,level);
        return "redirect:/teacher/HomeworkSubmitDetail?courseName="+courseName+"&homeworkTitle="+homeworkTitle+"&homeworkId="+homeworkId;
    }

    //我的授课
    @RequestMapping("/allMyClass")
    public String teacherAllClass(HttpServletRequest request, Model model){
        int teacherId = (int) request.getSession().getAttribute("userId");
        List<Course> list= courseService.getCourseByTeacherId(teacherId);
        model.addAttribute("list",list);
        return "teacher/teacherClass";
    }
    //上传成绩
    @RequestMapping("/submitGrade")
    public String submitGrade(HttpServletRequest request, Model model){
        int teacherId = (int) request.getSession().getAttribute("userId");
        List<Course> list= courseService.getCourseByTeacherId(teacherId);
        model.addAttribute("list",list);
        return "teacher/teacherGradeClassList";
    }

    //查看花名册
    @RequestMapping("/allStudentClass")
    public String allStudentClass(HttpServletRequest request, String courseId, String courseName, Model model){
        request.getSession().setAttribute("courseName",courseName);
        List<Score> list = scoreService.selectStudentInClass(courseId);
        model.addAttribute("list",list);
        return "teacher/teacherRoster";
    }
    //进入班级学生成绩详情页
    @RequestMapping("/studentGradeDetail")
    public String studentGradeDetail(HttpServletRequest request, String courseId, Model model){
        List<Score> list = scoreService.selectStudentInClass(courseId);
        model.addAttribute("list",list);
        request.getSession().setAttribute("courseId",courseId);
        return "teacher/teacherGradeStudentList";
    }
    //修改学生成绩
    @RequestMapping("/updateScore")
    public String updateScore(HttpServletRequest request, String score, String studentId){
        String courserId = request.getParameter("courseId");
        int scoreTemp = Integer.parseInt(score);
        int studentIdTemp = Integer.parseInt(studentId);
        scoreService.updateScoreByKey(studentIdTemp,courserId,scoreTemp);
        return "redirect:/teacher/studentGradeDetail?courseId="+courserId;
    }
    //老师添加课程
    @RequestMapping("/addClass")
    public String addClass(HttpServletRequest request){
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int teacherId = (int) request.getSession().getAttribute("userId");
        String courseId = request.getParameter("id");
        String courseName = request.getParameter("title");
        String courseDetail = request.getParameter("desc");
        courseService.addClass(courseId,courseName,teacherId,courseDetail);
        return "redirect:/teacher/allMyClass";
    }
    //老师进入发布作业详情页
    @RequestMapping("/AssignHomework")
    public String AssignHomework(HttpServletRequest request, String courseId, String courseName){
        request.getSession().setAttribute("courseId",courseId);
        request.getSession().setAttribute("courseName",courseName);
        return "teacher/teacherAssignHomework";
    }
    //发布作业
    @RequestMapping("/submitHomework")
    public String submitHomework(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String courseId = request.getParameter("courseId");
        String homeworkTitle = request.getParameter("desc");
        homeworkService.insertHomework(courseId,homeworkTitle);
        return "redirect:/teacher/allMyClass";
    }
    //删除作业
    @RequestMapping("/deleteHomework")
    public String deleteCourse(String homeworkId){
        int id = Integer.parseInt(homeworkId);
        homeworkService.deleteHoemwork(id);
        return "redirect:/teacher/viewHomeworkList";
    }
}
